//
//  lxLabel.m
//  luodansc
//
//  Created by 倫 星 on 2012/10/15.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxLabel.h"

@implementation lxLabel
@synthesize isWithStrikeThrough = _isWithStrikeThrough;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    if (YES == _isWithStrikeThrough) {
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGFloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
        CGContextSetStrokeColor(c, black);
        CGContextSetLineWidth(c, 1);
        CGContextBeginPath(c);
        CGFloat halfWayUp = (self.bounds.size.height - self.bounds.origin.y) / 2.0;
        CGContextMoveToPoint(c, self.bounds.origin.x, halfWayUp);
        CGContextAddLineToPoint(c, self.bounds.origin.x + self.bounds.size.width, halfWayUp);
        CGContextStrokePath(c);
    }
    int size[] = {96, 72, 64, 48, 36, 24, 18, 14, 13, 12, 11, 10}; //这么多字体，从大到小挨个尝试
    int array_length = 12;
    UIFont *font = nil;
    for(int i = 0; i < array_length; i++) {
        font = [font fontWithSize:size[i]];
        //设定宽度，高度无限高
//        CGSize constraintSize = CGSizeMake(self.bounds.size.width, MAXFLOAT);
        CGSize constraintSize = self.bounds.size;
        
        //计算实际需要得视图大小
        CGSize labelSize = [self.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByCharWrapping];
        
        //因为宽度已经限定了，所以判断高度是否符合需求，如果不符合，继续找更小得字体，如果符合，跳出循环
        if(labelSize.height < self.bounds.size.height - 0.2f && labelSize.width < self.bounds.size.width - 0.2f) {
            break;
        }
    }
    self.font = font;
    [super drawRect:rect];
}

- (void)dealloc {
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
